package com.thread;

public class DaemonThreadDemo {
    public static void main(String[] args) {
        Thread rose = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("Rose:let me go!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
                System.out.println("噗通");
            }
        });


        Thread jack = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("Jack:I love you!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        rose.start();
        //设置为守护线程,必须在开启之前设置
        jack.setDaemon(true);
        jack.start();

        //如果让主线程死循环，则进程不会结束，也就不会因为rose结束而杀死守护线程
        // 因为main也是一个线程，当用户线程全部结束时，守护线程也会自动结束
        //while (true);
    }
}
